<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用最新Thinkphp5助手函数特性实现单字母函数M D U等简写方式
 * ============================================================================
 * Author: IT宇宙人     
 * Date: 2015-09-09
 */
namespace app\admin\controller;
use app\admin\logic\OrderLogic;
use think\AjaxPage;
use think\console\command\make\Model;
use think\Page;
use think\Verify;
use think\Db;
use app\admin\logic\UsersLogic;
use app\common\model\Withdrawals;
use app\common\model\DisPost;
use think\Loader;

class Discuz extends Base {
    
    public function index(){
       return $this->fetch();
    }


    public function ajaxindex(){
        // 搜索条件
        $condition = array('status'=>1);
        $nickname = I('nickname');
        $title = I('title');

        $account ? $condition['title'] = ['like',"%$title%"] : false;
        $nickname ? $condition['nickname'] = ['like',"%$nickname%"] : false;
        $DiscuzModel = new DisPost();
        
        if(empty($condition)){
            $count = $DiscuzModel->where($condition)->count();
        }else{
            $count = Db::name('DisPost')->alias('d')->field('d.*,u.nickname')->join('__USERS__ u', 'u.user_id = d.uid', 'INNER')->where($condition)->count();
        }
        
        $Page  = new AjaxPage($count,10);
        $sort_order = I('order_by').' '.I('sort');
        // $userList = $DiscuzModel->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
        $userList = Db::name('DisPost')->alias('d')->field('d.*,u.nickname')->join('__USERS__ u', 'u.user_id = d.uid', 'INNER')->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();


        $show = $Page->show();
        foreach ($userList as $k => $v) {
            foreach ($v as $ke => $va) {
                if($ke == 'subTime'){
                    $userList[$k][$ke] = date("Y-m-d H:i:s",$va);
                }
            }
        }
        $this->assign('userList',$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }

    //
    public function add_post(){
       if(IS_POST){
            $data = I('post.');
            $data['status'] = 1;
            $username = Db::name('users')->field('nickname')->where('user_id='.$data['uid'])->find();
            $data['subTime'] = time();
            $data['username'] = $username['nickname'];
            $post = M('DisPost')->add($data);
            if($post){
                $this->success('添加成功',U('Discuz/index'));exit;
            }else{
                $this->error('添加失败',U('Discuz/index'));exit;
            }
        }
        $users = Db::name('users')->field('user_id,nickname')->select();
        $this->assign('users',$users);
        return $this->fetch();
    }


    //编辑话题
    public function detail(){
        $uid = I('get.id');
        $dpost = Db::name('DisPost')->alias('d')->field('d.*,u.nickname')->join('__USERS__ u', 'u.user_id = d.uid', 'INNER')->where('d.id='.$uid)->find();
        if(!$dpost)
            exit($this->error('话题不存在'));
        if(IS_POST){
            $row = M('dis_post')->where(array('id'=>$uid))->save($_POST);
            if($row)
                exit($this->success('修改成功',U('Discuz/index')));
            exit($this->error('未作内容修改或修改失败'));
        }
 
        $this->assign('dpost',$dpost);
        return $this->fetch();
    }



    //话题讨论列表
    public function reply(){
        $pid = I('get.id');
        $count = Db::name('DisReply')->where('pid='.$pid)->count();
        $Page  = new AjaxPage($count,10);
        $show = $Page->show();

        $reply = Db::name('DisReply')->alias('d')->field('d.*,u.title,u.content as cot')->join('DisPost u', 'u.id = d.pid', 'INNER')->where('d.pid='.$pid)->limit($Page->firstRow.','.$Page->listRows)->select();
        // $reply = Db::name('DisReply')->where('pid='.$pid)->select();
        if(!$reply){
            exit($this->error('这个话题没有人在讨论'));
        }
        $this->assign('reply',$reply);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }





    //编辑
    public function rpdetail(){
        $uid = I('get.id');
        $dpost = Db::name('DisReply')->where('id='.$uid)->find();
        if(!$dpost)
            exit($this->error('话题不存在'));
        if(IS_POST){
            var_dump($_POST);exit;
            $row = M('dis_reply')->where(array('id'=>$uid))->save($_POST);
            if($row)
                exit($this->success('修改成功',U('Discuz/index')));
            exit($this->error('未作内容修改或修改失败'));
        }
 
        $this->assign('dpost',$dpost);
        return $this->fetch();
    }



    public function add_reply(){
        if(IS_POST){
            $data = I('post.');
            if(empty($data['uid']) || empty($data['content'])){
                $this->error('添加失败,请选择发布人或者输入讨论内容',U('Discuz/index'));exit;
            }
            $username = Db::name('users')->field('nickname')->where('user_id='.$data['uid'])->find();
            $data['subTime'] = time();
            $data['username'] = $username['nickname'];
            $post = M('DisReply')->add($data);
            if($post){
                $this->success('添加成功',U('Discuz/index'));exit;
            }else{
                $this->error('添加失败',U('Discuz/index'));exit;
            }
        }
        $pid = I('get.id');
        $dpsot = Db::name('dis_post')->field('id,title,content')->where('id='.$pid)->find();
        $users = Db::name('users')->field('user_id,nickname')->select();
        $this->assign('users',$users);
        $this->assign('dpsot',$dpsot);
        return $this->fetch();
    }





    public function neighborhood(){
       return $this->fetch();
    }


    public function ajaxneighborhood(){
        // 搜索条件
        $condition = array('status'=>2);
        $nickname = I('nickname');
        $title = I('title');

        $account ? $condition['title'] = ['like',"%$title%"] : false;
        $nickname ? $condition['nickname'] = ['like',"%$nickname%"] : false;
        $DiscuzModel = new DisPost();
        
        if(empty($condition)){
            $count = $DiscuzModel->where($condition)->count();
        }else{
            $count = Db::name('DisPost')->alias('d')->field('d.*,u.nickname')->join('__USERS__ u', 'u.user_id = d.uid', 'INNER')->where($condition)->count();
        }
        
        $Page  = new AjaxPage($count,10);
        $sort_order = I('order_by').' '.I('sort');
        // $userList = $DiscuzModel->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
        $userList = Db::name('DisPost')->alias('d')->field('d.*,u.nickname')->join('__USERS__ u', 'u.user_id = d.uid', 'INNER')->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();
        foreach ($userList as $k => $v) {
            foreach ($v as $ke => $va) {
                if($ke == 'subTime'){
                    $userList[$k][$ke] = date("Y-m-d H:i:s",$va);
                }
            }
        }
        $this->assign('userList',$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }



    public function community(){
       return $this->fetch();
    }


    public function ajaxcommunity(){
        // 搜索条件
        $condition = array('status'=>3);
        $nickname = I('nickname');
        $title = I('title');

        $account ? $condition['title'] = ['like',"%$title%"] : false;
        $nickname ? $condition['nickname'] = ['like',"%$nickname%"] : false;
        $DiscuzModel = new DisPost();
        
        if(empty($condition)){
            $count = $DiscuzModel->where($condition)->count();
        }else{
            $count = Db::name('DisPost')->alias('d')->field('d.*,u.nickname')->join('__USERS__ u', 'u.user_id = d.uid', 'INNER')->where($condition)->count();
        }
        
        $Page  = new AjaxPage($count,10);
        $sort_order = I('order_by').' '.I('sort');
        // $userList = $DiscuzModel->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
        $userList = Db::name('DisPost')->alias('d')->field('d.*,u.nickname')->join('__USERS__ u', 'u.user_id = d.uid', 'INNER')->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();


        $show = $Page->show();
        foreach ($userList as $k => $v) {
            foreach ($v as $ke => $va) {
                if($ke == 'subTime'){
                    $userList[$k][$ke] = date("Y-m-d H:i:s",$va);
                }
            }
        }
        $this->assign('userList',$userList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }







    //邻里互动
    public function add_neighborhood(){
       if(IS_POST){
            $data = I('post.');
            $data['status'] = 2;
            $username = Db::name('users')->field('nickname')->where('user_id='.$data['uid'])->find();
            $data['subTime'] = time();
            $data['username'] = $username['nickname'];
            $post = M('DisPost')->add($data);
            if($post){
                $this->success('添加成功',U('Discuz/neighborhood'));exit;
            }else{
                $this->error('添加失败',U('Discuz/neighborhood'));exit;
            }
        }
        $users = Db::name('users')->field('user_id,nickname')->select();
        $this->assign('users',$users);
        return $this->fetch();
    }



     //社区活动
    public function add_community(){
       if(IS_POST){
            $data = I('post.');
            $data['status'] = 3;
            $username = Db::name('users')->field('nickname')->where('user_id='.$data['uid'])->find();
            $data['subTime'] = time();
            $data['username'] = $username['nickname'];
            $post = M('DisPost')->add($data);
            if($post){
                $this->success('添加成功',U('Discuz/community'));exit;
            }else{
                $this->error('添加失败',U('Discuz/community'));exit;
            }
        }
        $users = Db::name('users')->field('user_id,nickname')->select();
        $this->assign('users',$users);
        return $this->fetch();
    }



    //编辑话题
    public function det_neighborhood(){
        $uid = I('get.id');
        $dpost = Db::name('DisPost')->alias('d')->field('d.*,u.nickname')->join('__USERS__ u', 'u.user_id = d.uid', 'INNER')->where('d.id='.$uid)->find();
        if(!$dpost)
            exit($this->error('话题不存在'));
        if(IS_POST){
            $row = M('dis_post')->where(array('id'=>$uid))->save($_POST);
            if($row)
                exit($this->success('修改成功',U('Discuz/neighborhood')));
            exit($this->error('未作内容修改或修改失败'));
        }
 
        $this->assign('dpost',$dpost);
        return $this->fetch();
    }


     //编辑话题
    public function del_neighborhood(){
        $uid = I('get.id');
        if($uid)
            $row = M('dis_post')->where(array('id'=>$uid))->delete();
            if($row)
                exit($this->success('删除成功',U('Discuz/neighborhood')));
            exit($this->error('删除失败'));

        $this->assign('dpost',$dpost);
        return $this->fetch();
    }

    //编辑话题
    public function det_community(){
        $uid = I('get.id');
        $dpost = Db::name('DisPost')->alias('d')->field('d.*,u.nickname')->join('__USERS__ u', 'u.user_id = d.uid', 'INNER')->where('d.id='.$uid)->find();
        if(!$dpost)
            exit($this->error('话题不存在'));
        if(IS_POST){
            $row = M('dis_post')->where(array('id'=>$uid))->save($_POST);
            if($row)
                exit($this->success('修改成功',U('Discuz/community')));
            exit($this->error('未作内容修改或修改失败'));
        }
 
        $this->assign('dpost',$dpost);
        return $this->fetch();
    }


     //删除
    public function del_community(){
        $uid = I('get.id');
        if($uid)
            $row = M('dis_post')->where(array('id'=>$uid))->delete();
            if($row)
                exit($this->success('删除成功',U('Discuz/community')));
            exit($this->error('删除失败'));
 
        $this->assign('dpost',$dpost);
        return $this->fetch();
    }
}